package yxy.game.pm2.module.daily;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.signtask;
import yxy.cherry.data.bean.task;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.task.Task;

import java.util.ArrayList;
import java.util.Map;

public class DailyModule extends PlayerModule {
	// bean===========================================================================
	private int exp;// 每日任务经验
	private ArrayList<String> taskCodes = new ArrayList<>();// 任务列表
	private ArrayList<Integer> levelReward = new ArrayList<>();// 领过的阶段奖励

	public int getExp() {
		return exp;
	}

	public void setExp(int exp) {
		this.exp = exp;
	}

	public ArrayList<String> getTaskCodes() {
		return taskCodes;
	}

	public void setTaskCodes(ArrayList<String> taskCodes) {
		this.taskCodes = taskCodes;
	}

	public ArrayList<Integer> getLevelReward() {
		return levelReward;
	}

	public void setLevelReward(ArrayList<Integer> levelReward) {
		this.levelReward = levelReward;
	}
	// func===========================================================================

	private void update() {
		DateTime now = DateTime.now();
		DateTime time = timeline().getStartTime();
		if (time == null || !new LocalDate(now).equals(new LocalDate(time))) {
			try {
				if (tryLock() && Task.lockTask(getPlayerCode())) {
					timeline().setStartTime(now);
					levelReward.clear();
					Task.remove(getPlayerCode(), getTaskCodes());
					taskCodes.clear();
					setExp(0);
					Map<String, signtask> datas = DataCenter.getAllData(signtask.class);
					for (signtask data : datas.values()) {
						int taskId = data.getTaskid();
						task taskdata = DataCenter.getData(taskId, task.class);
						Task task = Task.create(getPlayerCode(), taskdata);
						if (task != null) {
							getTaskCodes().add(task.getCode());
						}
					}
					this.set();
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				unlock();
				Task.unlockTask(getPlayerCode());
			}
		}
	}

	public void addexp(int exp) {
		this.exp += exp;
		set();
	}
	// redis==========================================================================

	private static String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:DAILY", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public DailyModule get(String playerCode) {
		try {
			DailyModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), DailyModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			module.update();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static private DailyModule create(String playerCode) {
		DailyModule module = new DailyModule();
		module.setPlayerCode(playerCode);
		module.update();

		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				DailyModule module = getv(key(playerCode));
				MySQLCacheDAO.setV(key, module);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
